#pragma once

#ifndef __LIT__MESH_DESC__FILE__HH__
#define __LIT__MESH_DESC__FILE__HH__

#include <Ogre.h>
#include <LitPrerequisites.h>
#include <tinyxml.h>
#include <LitPhysicsTypes.h>

namespace Lit
{
	class MeshDescriptionSerializer;
	class LIT_API MeshDescriptionFile : public Ogre::Resource
	{
	protected:
		friend class MeshDescriptionSerializer;
		// must implement these from the Ogre::Resource interface
		void loadImpl();
		void unloadImpl();
		size_t calculateSize() const;

		Physics::ShapeType mShape;
		Ogre::Real mMass;
		Lit::Vec3 mFacingDir;
		Lit::Vec3 mOffset;
		std::string mMaterial;
		std::string mMesh;
		bool mDynamic;

	public:
		MeshDescriptionFile(Ogre::ResourceManager *creator, const Ogre::String &name, 
			Ogre::ResourceHandle handle, const Ogre::String &group, bool isManual = false, 
			Ogre::ManualResourceLoader *loader = 0);
		MeshDescriptionFile( const MeshDescriptionFile &rk );
		virtual ~MeshDescriptionFile( );
		
		bool isDynamic() const;
		Physics::ShapeType getShape() const;
		Ogre::Real getMass() const;
		const Lit::Vec3& getFacingDirection() const;
		const Lit::Vec3& getOffset() const;
		const std::string& getMesh() const;
		const std::string& getMaterial() const;

	public:
	};
 
	class MeshDescriptionFilePtr : public Ogre::SharedPtr<MeshDescriptionFile> 
	{
	public:
		MeshDescriptionFilePtr();
		explicit MeshDescriptionFilePtr(MeshDescriptionFile *rep);
		MeshDescriptionFilePtr(const MeshDescriptionFilePtr &r);
		MeshDescriptionFilePtr(const Ogre::ResourcePtr &r);
 
		/// Operator used to convert a ResourcePtr to a ScenePtr
		MeshDescriptionFilePtr& operator=(const Ogre::ResourcePtr& r);
	};
	class MeshDescriptionSerializer : public Ogre::Serializer
	{
	public:
		MeshDescriptionSerializer();
		virtual ~MeshDescriptionSerializer();
 
		void exportDescription(const MeshDescriptionFile *pText, const Ogre::String &fileName);
		void importDescription(Ogre::DataStreamPtr &stream, MeshDescriptionFile *pDest);
	}; 
	class LIT_API MeshDescriptionManager : public Ogre::ResourceManager
	{
	protected:
		static MeshDescriptionManager * ms_singleton;
		// must implement this from ResourceManager's interface
		Ogre::Resource *createImpl(const Ogre::String &name, Ogre::ResourceHandle handle, 
			const Ogre::String &group, bool isManual, Ogre::ManualResourceLoader *loader, 
			const Ogre::NameValuePairList *createParams);

	public:
		MeshDescriptionManager();
		virtual ~MeshDescriptionManager();

		virtual MeshDescriptionFilePtr load(const Ogre::String &name, const Ogre::String &group = Ogre::ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME);
		
		static MeshDescriptionManager &getSingleton();
		static MeshDescriptionManager *getSingletonPtr();
		
		const Ogre::StringVector& getScriptPatterns(void) const;
		void parseScript(Ogre::DataStreamPtr& stream, const Ogre::String& groupName);
		Ogre::Real getLoadingOrder(void) const;

		static std::string RESOURCE_TYPE;
	};

	typedef RefPtr<MeshDescriptionManager> MeshDescriptionManagerPtr;
};

#endif